home *** CD-ROM | disk | FTP | other *** search
/ isnet Internet / Isnet Internet CD.iso / prog / hiz / 09 / 09.exe / adynware.exe / perl / lib / Net / netent.pm < prev    next >
Encoding:
Perl POD Document  |  1999-12-28  |  4.3 KB  |  164 lines

  1. package Net::netent;
  2. use strict;
  3.  
  4. BEGIN { 
  5.     use Exporter   ();
  6.     use vars       qw(@EXPORT @EXPORT_OK %EXPORT_TAGS);
  7.     @EXPORT      = qw(getnetbyname getnetbyaddr getnet);
  8.     @EXPORT_OK   = qw(
  9.             $n_name            @n_aliases
  10.             $n_addrtype     $n_net
  11.            );
  12.     %EXPORT_TAGS = ( FIELDS => [ @EXPORT_OK, @EXPORT ] );
  13. }
  14. use vars      @EXPORT_OK;
  15.  
  16. sub import { goto &Exporter::import }
  17.  
  18. use Class::Struct qw(struct);
  19. struct 'Net::netent' => [
  20.    name        => '$',
  21.    aliases    => '@',
  22.    addrtype    => '$',
  23.    net        => '$',
  24. ];
  25.  
  26. sub populate (@) {
  27.     return unless @_;
  28.     my $nob = new();
  29.     $n_name      =    $nob->[0]              = $_[0];
  30.     @n_aliases     = @{ $nob->[1] } = split ' ', $_[1];
  31.     $n_addrtype  =    $nob->[2]          = $_[2];
  32.     $n_net     =    $nob->[3]          = $_[3];
  33.     return $nob;
  34.  
  35. sub getnetbyname ($)  { populate(CORE::getnetbyname(shift)) } 
  36.  
  37. sub getnetbyaddr ($;$) { 
  38.     my ($net, $addrtype);
  39.     $net = shift;
  40.     require Socket if @_;
  41.     $addrtype = @_ ? shift : Socket::AF_INET();
  42.     populate(CORE::getnetbyaddr($net, $addrtype)) 
  43.  
  44. sub getnet($) {
  45.     if ($_[0] =~ /^\d+(?:\.\d+(?:\.\d+(?:\.\d+)?)?)?$/) {
  46.     require Socket;
  47.     &getnetbyaddr(Socket::inet_aton(shift));
  48.     } else {
  49.     &getnetbyname;
  50.     } 
  51.  
  52. 1;
  53. __END__
  54.  
  55. =head1 NAME
  56.  
  57. Net::netent - by-name interface to Perl's built-in getnet*() functions
  58.  
  59. =head1 SYNOPSIS
  60.  
  61.  use Net::netent qw(:FIELDS);
  62.  getnetbyname("loopback")         or die "bad net";
  63.  printf "%s is %08X\n", $n_name, $n_net;
  64.  
  65.  use Net::netent;
  66.  
  67.  $n = getnetbyname("loopback")         or die "bad net";
  68.  { # there's gotta be a better way, eh?
  69.      @bytes = unpack("C4", pack("N", $n->net));
  70.      shift @bytes while @bytes && $bytes[0] == 0;
  71.  }
  72.  printf "%s is %08X [%d.%d.%d.%d]\n", $n->name, $n->net, @bytes;
  73.  
  74. =head1 DESCRIPTION
  75.  
  76. This module's default exports override the core getnetbyname() and
  77. getnetbyaddr() functions, replacing them with versions that return
  78. "Net::netent" objects.  This object has methods that return the similarly
  79. named structure field name from the C's netent structure from F<netdb.h>;
  80. namely name, aliases, addrtype, and net.  The aliases 
  81. method returns an array reference, the rest scalars.  
  82.  
  83. You may also import all the structure fields directly into your namespace
  84. as regular variables using the :FIELDS import tag.  (Note that this still
  85. overrides your core functions.)  Access these fields as variables named
  86. with a preceding C<n_>.  Thus, C<$net_obj-E<gt>name()> corresponds to
  87. $n_name if you import the fields.  Array references are available as
  88. regular array variables, so for example C<@{ $net_obj-E<gt>aliases()
  89. }> would be simply @n_aliases.
  90.  
  91. The getnet() funtion is a simple front-end that forwards a numeric
  92. argument to getnetbyaddr(), and the rest
  93. to getnetbyname().
  94.  
  95. To access this functionality without the core overrides,
  96. pass the C<use> an empty import list, and then access
  97. function functions with their full qualified names.
  98. On the other hand, the built-ins are still available
  99. via the C<CORE::> pseudo-package.
  100.  
  101. =head1 EXAMPLES
  102.  
  103. The getnet() functions do this in the Perl core:
  104.  
  105.     sv_setiv(sv, (I32)nent->n_net);
  106.  
  107. The gethost() functions do this in the Perl core:
  108.  
  109.     sv_setpvn(sv, hent->h_addr, len);
  110.  
  111. That means that the address comes back in binary for the
  112. host functions, and as a regular perl integer for the net ones.
  113. This seems a bug, but here's how to deal with it:
  114.  
  115.  use strict;
  116.  use Socket;
  117.  use Net::netent;
  118.  
  119.  @ARGV = ('loopback') unless @ARGV;
  120.  
  121.  my($n, $net);
  122.  
  123.  for $net ( @ARGV ) {
  124.  
  125.      unless ($n = getnetbyname($net)) {
  126.      warn "$0: no such net: $net\n";
  127.      next;
  128.      }
  129.  
  130.      printf "\n%s is %s%s\n", 
  131.          $net, 
  132.          lc($n->name) eq lc($net) ? "" : "*really* ",
  133.          $n->name;
  134.  
  135.      print "\taliases are ", join(", ", @{$n->aliases}), "\n"
  136.          if @{$n->aliases};     
  137.  
  138.      {
  139.      my @a = unpack("C4", pack("N", $n->net));
  140.      shift @a while @a && $a[0] == 0;
  141.      printf "\taddr is %s [%d.%d.%d.%d]\n", $n->net, @a;
  142.      }
  143.  
  144.      if ($n = getnetbyaddr($n->net)) {
  145.      if (lc($n->name) ne lc($net)) {
  146.          printf "\tThat addr reverses to net %s!\n", $n->name;
  147.          $net = $n->name;
  148.          redo;
  149.      } 
  150.      }
  151.  }
  152.  
  153. =head1 NOTE
  154.  
  155. While this class is currently implemented using the Class::Struct
  156. module to build a struct-like class, you shouldn't rely upon this.
  157.  
  158. =head1 AUTHOR
  159.  
  160. Tom Christiansen
  161.